import { Event } from "../../utils/interface";
import { observer } from "../../utils/observer";

const tabs = ["体验问题", "商品,商家投诉"];
const types = [
  { name: "功能建议", flag: false },
  { name: "购买的问题", flag: false },
  { name: "性能问题", flag: false },
  { name: "其他", flag: false },
];
let imgs: Array<{ path: string; size: number }> = [];
let text = "";

let data = {
  types,
  tabs,
  text,
  imgs,
};
Page({
  data,
  onLoad() {
    setTimeout(() => {
      data = observer(this.data, this);
      console.log(data);
    });
  },
  blur(e: Event<unknown>) {
    data.text = e.detail.value;
  },
  changeTab(e: Event<unknown>) {
    console.log(e.detail);
  },
  upload() {
    wx.chooseImage({
      success(e) {
        data.imgs.push(...e.tempFiles);
      },
    });
  },
  changeFlag(e: Event<{ item: { name: string; flag: boolean } }>) {
    const row = e.currentTarget.dataset.item;
    data.types = data.types.map((item) => {
      if (item.name === row.name) {
        item.flag = !row.flag;
      }
      return item;
    });
  },
  submit() {
    const info = {
      types: data.types.filter((item) => item.flag),
      value: data.text,
      imgs: data.imgs,
    };
    console.log(info);
    wx.showLoading({ title: "正在提交反馈" });
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: "提交成功",
        icon: "success",
        duration: 1000,
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1000);
    }, 1500);
  },
});
